using System;
using MediaPortal.GUI.Library;
using System.Text.RegularExpressions;
using System.Net;
using System.Text;
using MediaPortal.Player;
using System.Collections.Generic;
//using MediaPortal.Utils.Services;
using MediaPortal.GUI.View ;
using MediaPortal.Dialogs;
using System.Xml;
using System.Xml.XPath;
using System.ComponentModel;
using System.Threading;



namespace OnlineVideos
{
	public class BreakUtil:SiteUtilBase
	{
		public override string getSiteId()
		{
			return "22";
		}
		
		protected override String getUrl(String fsId)
		{	String lsUrl = "";
			String lsHtml = getHTMLData(fsId);
			//Log.Write("html to parse to get url = \n{0}",lsHtml);
			Regex loPathRegex = new Regex("sGlobalFileName='([^']*)';[^;]*;.+sGlobalContentFilePath='([^']*)'");
			Regex loUrlRegex = new Regex(@"so.addVariable\('videoPath',\s'([^']*)");
			Match urlField = loUrlRegex.Match(lsHtml);
			if(urlField.Success){			
				lsUrl = urlField.Groups[1].Value;
				Match loMatch = loPathRegex.Match(lsHtml);
				if(loMatch.Success){
					String lsFileName = loMatch.Groups[1].Value;
					String lsPathName = loMatch.Groups[2].Value;
					lsUrl = lsUrl+lsPathName+"/"+lsFileName+".flv";
					Log.Info("break flv url = {0}",lsUrl);
				}
			}
			return lsUrl;
		}
		public override List<GUIOnlineVideos.VideoInfo> getVideoList(string fsUrl)
		{
			List<RssItem> loRssItemList = getRssDataItems(fsUrl);
			List<GUIOnlineVideos.VideoInfo> loVideoList = new List<GUIOnlineVideos.VideoInfo>();
			GUIOnlineVideos.VideoInfo video;
			foreach(RssItem rssItem in loRssItemList){
				video = new GUIOnlineVideos.VideoInfo();
				video.Description = rssItem.description;
				video.ImageUrl = rssItem.enclosure;
				video.Title = rssItem.title;
				//foreach(MediaContent content in rssItem.contentList){
				//	if(content.type.Contains("flv")){
				video.VideoUrl = rssItem.link;
				//		break;
				//	}
				loVideoList.Add(video);
			}
			return loVideoList;
		}
		/*
		public List<GUIOnlineVideos.VideoInfo> getRssData(String fsUrl)
		{
			
			XmlDocument doc = new XmlDocument();
			//XPathDocument doc = new XPathDocument(fsUrl);

			doc.Load(XmlReader.Create(fsUrl));
			//XmlNamespaceManager expr = new XmlNamespaceManager(doc.NameTable);
			//expr.AddNamespace("", "http://search.yahoo.com/mrss");
			//Console.WriteLine(doc.InnerXml);
			//Create the root XmlNode and generate an XmlNodeList from it.
			XmlNode root = doc.SelectSingleNode("//rss/channel/item");
			if(root==null){
				Console.WriteLine(root);
				
			}
			XmlNodeList nodeList;
			nodeList = root.SelectNodes("//rss/channel/item");
			//RssItem loRssItem = new RssItem();
			//loRssItem.
			//This loops through each node found in the nodeList and adds the
			//Node data to the DataTable.
			XmlAttributeCollection ac;
			List<GUIOnlineVideos.VideoInfo> loRssItems = new List<GUIOnlineVideos.VideoInfo>();
			GUIOnlineVideos.VideoInfo loRssItem;
			//GUIListItem loListItem;
			foreach (XmlNode chileNode in nodeList)
			{
				loRssItem = new GUIOnlineVideos.VideoInfo();

				XmlNode node = chileNode.SelectSingleNode("title");
				loRssItem.Title = node.InnerText;

				node = chileNode.SelectSingleNode("link");
				loRssItem.VideoUrl = node.InnerText;

				node = chileNode.SelectSingleNode("description");
				loRssItem.Description = node.InnerText;

				node = chileNode.SelectSingleNode("enclosure");
				if (node != null)
				{
					ac = node.Attributes;
					loRssItem.ImageUrl = ac["url"].InnerText;
				}
				loRssItems.Add(loRssItem);

				
			}
			
			return loRssItems;
		}
		*/
		
		
		
	}
}
